package com.yanqu.road.server.cmd.activity.huashigang;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorCellData;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangUserFloor;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.huashigang.HuaShiGangProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.huashigang.HuaShiGangModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.huashigang.HuaShiGangMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
@Cmd(code = GameProtocol.S_HUA_SHI_GANG_DIG, desc = "花石纲挖宝库")
public class DigHuaShiGangFloorDataCmd extends AbstractCommand {

    int digId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HuaShiGangProto.HuaShiGangDigFloorReqMsg msg = HuaShiGangProto.HuaShiGangDigFloorReqMsg.parseFrom(packet.getBytes());
        digId = msg.getDigId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = HuaShiGangMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_HUA_SHI_GAG_NOT_IN_FIGHT_TIME;
            return;
        }
        int boxId = HuaShiGangMgr.getNewConfig().getDigPassBox();

        HuaShiGangModule huaShiGangModule = player.getModule(HuaShiGangModule.class);
        huaShiGangModule.checkFloor(activityInfo.getActivityId(), boxId);

        HuaShiGangUserFloor huaShiGangUserFloor = huaShiGangModule.getFloorData(activityInfo.getActivityId());
        HuaShiGangFloorCellData cellData = huaShiGangUserFloor.getCellDataList().get(digId - 1);
        if (cellData.isOpen()) {
            ret = GameErrorCode.E_HUA_SHI_GANG_FLOOR_CELL_OPEN;
            return;
        }

        CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
        HuaShiGangFloorConfig config = HuaShiGangMgr.getHuaShiGangFloorConfig(huaShiGangUserFloor.getFloor());
        if (!currencyModule.removeCurrency(PropertyHelper.parseStringToProperty(config.getConsume()), eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangDIG)) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        // 设置打开状态
        cellData.setOpen(true);
        huaShiGangUserFloor.setUpdateOption();

        // 获得奖励
        Property property = PropertyHelper.parseStringToProperty(cellData.getReward());
        currencyModule.addCurrency(property, eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangDIG);

        // 判断是否进入下一层 进入下一层则有通关大奖
        property.addProperty(huaShiGangModule.checkFloor(activityInfo.getActivityId(), boxId));

        HuaShiGangProto.HuaShiGangDigFloorRespMsg.Builder builder = HuaShiGangProto.HuaShiGangDigFloorRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setReward(PropertyHelper.parsePropertyToString(property));
        builder.setSpecial(cellData.isSpecial());

        player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_DIG, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HuaShiGangProto.HuaShiGangDigFloorRespMsg.Builder builder = HuaShiGangProto.HuaShiGangDigFloorRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HUA_SHI_GANG_DIG, builder);
        }
    }
}
